package com.example.solrdemo.solrtest;

import com.example.solrdemo.entity.User;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SolrTest {
    private SolrClient solrClient;

    @Before
    public void before(){
        solrClient = new HttpSolrClient.Builder("http://localhost:6789/solr/mote").build();
    }
    //添加单个对象到索引库
    @Test
    public void addObject() throws IOException, SolrServerException {
        User user = new User();
        user.setId("1");
        user.setUsername("小明");
        solrClient.addBean(user);
        solrClient.commit();
    }
    //批量添加对象
    @Test
    public void addList() throws IOException, SolrServerException {
        List<User> list = new ArrayList<>();
        for (int i=0; i<100; i++){
            User user = new User();
            user.setId(i+1+"");
            user.setUsername("人杰地灵"+i);
            list.add(user);
        }
        solrClient.addBeans(list);
        solrClient.commit();
    }
    //通过document添加数据
    @Test
    public void addByDocument() throws IOException, SolrServerException {
        SolrInputDocument solrInputFields = new SolrInputDocument();
        solrInputFields.addField("id","5");
        solrInputFields.addField("name","我不是天下第一");
        solrClient.add(solrInputFields);
        solrClient.commit();
    }
    //两种删除方式
    @Test
    public void delete() throws IOException, SolrServerException {
        //删除的第一种方式
        solrClient.deleteById("1");
        //删除的第二种方式
        solrClient.deleteByQuery("name:我不是天下第一");
        solrClient.commit();
    }
    //查询
    @Test
    public void query() throws IOException, SolrServerException {
        //构建搜索条件
        SolrQuery solrQuery = new SolrQuery();
        //设置搜索关键字
        solrQuery.setQuery("name:我不是天下第一");
        //设置排序
        solrQuery.setSort("id",SolrQuery.ORDER.desc);
        //设置分页信息
        solrQuery.setStart(0);
        solrQuery.setRows(10);
        //设置高亮
        solrQuery.setHighlight(true);//开启高亮组件
        solrQuery.addHighlightField("name");//添加高亮字段
        solrQuery.setHighlightSimplePre("<em style='color:red'>");//设置高亮前缀
        solrQuery.setHighlightSimplePost("</em>");//设置高亮后缀
        //执行查询
        QueryResponse response = solrClient.query(solrQuery);
        List<User> users = response.getBeans(User.class);
        //将高亮标识写进name标识字段上
        Map<String,Map<String,List<String>>> map = response.getHighlighting();
        for(Map.Entry<String,Map<String,List<String>>> entry: map.entrySet()){
            for(User user : users){
                if(!entry.getKey().equals(user.getId().toString())){
                    continue;
                }
                user.setUsername(entry.getValue().get("name").toString());
                break;
            }
        }
        for (User user : users){
            System.out.println(user.toString());
        }
    }

}
